<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>表单页面</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/res/layui/css/layui.css" media="all">
    <script src="/res/layui/layui.js"></script>
    <script src="/res/js/tools.js"></script>
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>用户信息维护</legend>
</fieldset>

<form class="layui-form" action="" lay-filter="example" id="myform">
    <input type="hidden" name="id" id="id"><!-- 加一个隐藏主键 -->
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-inline">
                <input type="text" name="name" id="name" lay-verify="required" autocomplete="off"
                       placeholder="请输入用户名" class="layui-input">
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="button" class="layui-btn" lay-submit="" lay-filter="tijiao">立即提交</button>
            <button type="button" class="layui-btn layui-btn-primary" id="close_btn">关闭</button>
        </div>
    </div>

</form>

</body>
<script>
    layui.use(['form', 'layedit', 'laydate','jquery'], function() {
        var form = layui.form
            , layer = layui.layer
            , layedit = layui.layedit
            , laydate = layui.laydate
            , $ = layui.$;

        //监听提交
        form.on('submit(tijiao)', function(data){

            let url = "/person/add.action";
            let formData = new FormData(document.getElementById("myform"));
            console.info(formData);//输出表单数据
            $.ajax({
                type: 'POST',   //上传文件必须使用post
                url: url,
                data: formData, //需要传输的数据
                dataType:'json',
                processData: false, // 告诉jquery要传输data对象
                contentType: false,   // 告诉jquery不需要增加请求头对于contentType的设置
                success: function (resp) { //如果上传成功,就执行

                    if(resp.state==1){
                        //注意：parent 是 JS 自带的全局对象，可用于操作父页面
                        var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                        parent.layer.close(index);
                        //父页面刷新
                        parent.table.reload('dataListTable');

                        //父页面提示信息
                        parent.layer.msg(resp.msg);

                    }else{
                        layer.msg(resp.msg);
                    }

                }
                ,error:function (arg) { //如果上传失败,就执行
                    console.log(arg)
                }
            });

            return false;
        });

        $("#close_btn").on("click",function () {
            layerClose();
        });


        /*
        * 当点击编辑按钮时,会传入一个id到form.html页面
        * */
        let id = getQueryString("id");
        console.info(id);
        //如果id有值
        if(id){
            let url = "/person/showUpdate.action";
            let data_ = {id:id};
            $.post(url,data_,function (resp) {
                if(resp.state==1){
                    console.info(resp.data);
                    $("#name").val(resp.data.name);
                    $("#id").val(resp.data.id);
                    //重新渲染
                    form.render();

                }
            },"json");
        }
    });



</script>
</html>